
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>语法分析 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-codeblock-label/block.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-click-reveal/click_reveal.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part2-1-rdp.html" />
    
    
    <link rel="prev" href="part1-1-task.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    实验简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../log.html">
            
                <a href="../log.html">
            
                    
                    更新日志
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">零：前置准备</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    配环境、跑测试
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../lab0/env.html">
            
                <a href="../lab0/env.html">
            
                    
                    环境配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../lab0/testing.html">
            
                <a href="../lab0/testing.html">
            
                    
                    运行测试样例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../lab0/riscv.html">
            
                <a href="../lab0/riscv.html">
            
                    
                    RISC-V 的工具链使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">一：第一个编译器</li>
        
        
    
        <li class="chapter " data-level="3.1" >
            
                <span>
            
                    
                    仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="part0-intro.html">
            
                <a href="part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="part1-lex.html">
            
                <a href="part1-lex.html">
            
                    
                    词法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="part1-1-task.html">
            
                <a href="part1-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.1.4" data-path="part2-parse.html">
            
                <a href="part2-parse.html">
            
                    
                    语法分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="part2-1-rdp.html">
            
                <a href="part2-1-rdp.html">
            
                    
                    递归下降分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="part2-2-task.html">
            
                <a href="part2-2-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.7" data-path="part3-ir.html">
            
                <a href="part3-ir.html">
            
                    
                    中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.8" data-path="part3-1-task.html">
            
                <a href="part3-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.9" data-path="part4-codegen.html">
            
                <a href="part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.10" data-path="part4-1-task.html">
            
                <a href="part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.11" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    [dzy] 仅一个 return 的 main 函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../lab1alt/part1.html">
            
                <a href="../lab1alt/part1.html">
            
                    
                    第一部分：从零开始的 lexer、parser 以及汇编生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../lab1alt/part2.html">
            
                <a href="../lab1alt/part2.html">
            
                    
                    第二部分：词法语法分析工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../lab1alt/part3.html">
            
                <a href="../lab1alt/part3.html">
            
                    
                    第三部分：使用中间码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.4.1" data-path="../lab1alt/antlr.html">
            
                <a href="../lab1alt/antlr.html">
            
                    
                    ANTLR 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4.2" data-path="../lab1alt/visitor.html">
            
                <a href="../lab1alt/visitor.html">
            
                    
                    Visitor 模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">二：常量表达式</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <span>
            
                    
                    步骤二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../lab2/part0-intro.html">
            
                <a href="../lab2/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../lab2/summary.html">
            
                <a href="../lab2/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" >
            
                <span>
            
                    
                    步骤三
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../lab3/part0-intro.html">
            
                <a href="../lab3/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../lab3/summary.html">
            
                <a href="../lab3/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" >
            
                <span>
            
                    
                    步骤四
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../lab4/part0-intro.html">
            
                <a href="../lab4/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../lab4/summary.html">
            
                <a href="../lab4/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">三：变量和语句</li>
        
        
    
        <li class="chapter " data-level="5.1" >
            
                <span>
            
                    
                    步骤五
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../lab5/part0-intro.html">
            
                <a href="../lab5/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../lab5/stackframe.html">
            
                <a href="../lab5/stackframe.html">
            
                    
                    栈帧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="../lab5/summary.html">
            
                <a href="../lab5/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    步骤六
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.2.1" data-path="../lab6/part0-intro.html">
            
                <a href="../lab6/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2.2" data-path="../lab6/summary.html">
            
                <a href="../lab6/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">四：块语句和作用域和更多语句</li>
        
        
    
        <li class="chapter " data-level="6.1" >
            
                <span>
            
                    
                    步骤七
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../lab7/part0-intro.html">
            
                <a href="../lab7/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../lab7/namer.html">
            
                <a href="../lab7/namer.html">
            
                    
                    名称解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../lab7/summary.html">
            
                <a href="../lab7/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" >
            
                <span>
            
                    
                    步骤八
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../lab8/part0-intro.html">
            
                <a href="../lab8/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../lab8/part4-codegen.html">
            
                <a href="../lab8/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="../lab8/part4-1-task.html">
            
                <a href="../lab8/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.4" data-path="../lab8/summary.html">
            
                <a href="../lab8/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">五：函数和全局变量</li>
        
        
    
        <li class="chapter " data-level="7.1" >
            
                <span>
            
                    
                    步骤九
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../lab9/part0-intro.html">
            
                <a href="../lab9/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../lab9/part4-1-cconv.html">
            
                <a href="../lab9/part4-1-cconv.html">
            
                    
                    调用约定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../lab9/part4-2-codegen.html">
            
                <a href="../lab9/part4-2-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../lab9/part4-3-task.html">
            
                <a href="../lab9/part4-3-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../lab9/summary.html">
            
                <a href="../lab9/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" >
            
                <span>
            
                    
                    步骤十
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../lab10/part0-intro.html">
            
                <a href="../lab10/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../lab10/part4-codegen.html">
            
                <a href="../lab10/part4-codegen.html">
            
                    
                    代码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../lab10/part4-1-task.html">
            
                <a href="../lab10/part4-1-task.html">
            
                    
                    任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../lab10/summary.html">
            
                <a href="../lab10/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">六：指针和数组</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    步骤十一
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../lab11/part0-intro.html">
            
                <a href="../lab11/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../lab11/typeck.html">
            
                <a href="../lab11/typeck.html">
            
                    
                    类型检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../lab11/summary.html">
            
                <a href="../lab11/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8.2" >
            
                <span>
            
                    
                    步骤十二
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.2.1" data-path="../lab12/part0-intro.html">
            
                <a href="../lab12/part0-intro.html">
            
                    
                    摘要
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.2.2" data-path="../lab12/summary.html">
            
                <a href="../lab12/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">参考资料</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../REFERENCE.html">
            
                <a href="../../REFERENCE.html">
            
                    
                    参考资料
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >语法分析</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x6211;&#x4EEC;&#x4E0D;&#x80FD;&#x5BF9;&#x8D1F;&#x6574;&#x6570;&#x8FDB;&#x884C;&#x8BCD;&#x6CD5;&#x5206;&#x6790;&#x3002;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x5076;&#x7136;&#x7684;--C &#x8BED;&#x8A00;&#x6CA1;&#x6709;&#x8D1F;&#x6574;&#x6570;&#x5E38;&#x91CF;&#x3002;&#x5B83;&#x53EA;&#x662F;&#x6709;&#x4E00;&#x4E2A;&#x8D1F;&#x4E00;&#x5143;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x53EF;&#x4EE5;&#x5E94;&#x7528;&#x4E8E;&#x6B63;&#x6574;&#x6570;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x4E0B;&#x4E00;&#x6B65;&#x6DFB;&#x52A0;&#x8D1F;&#x4E00;&#x5143;&#x8FD0;&#x7B97;&#x3002;</p>
</blockquote>
<h2 id="&#x8BED;&#x6CD5;&#x89E3;&#x6790;">&#x8BED;&#x6CD5;&#x89E3;&#x6790;</h2>
<p>&#x4E0B;&#x4E00;&#x6B65;&#x662F;&#x5C06;&#x6211;&#x4EEC;&#x7684;&#x6807;&#x8BB0;&#x5217;&#x8868;&#x8F6C;&#x5316;&#x4E3A;&#x62BD;&#x8C61;&#x7684;&#x8BED;&#x6CD5;&#x6811;&#xFF08;Abstract  Syntax Tree&#xFF0C;&#x7B80;&#x79F0;AST&#xFF09;&#x3002;AST&#x662F;&#x8868;&#x793A;&#x7A0B;&#x5E8F;&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;&#x5728;&#x5927;&#x591A;&#x6570;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x50CF;&#x6761;&#x4EF6;&#x548C;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x8FD9;&#x6837;&#x7684;&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x662F;&#x7531;&#x66F4;&#x7B80;&#x5355;&#x7684;&#x7ED3;&#x6784;&#x7EC4;&#x6210;&#x7684;&#xFF0C;&#x6BD4;&#x5982;&#x53D8;&#x91CF;&#x548C;&#x5E38;&#x91CF;&#x3002;AST&#x6355;&#x6349;&#x5230;&#x4E86;&#x8FD9;&#x79CD;&#x5173;&#x7CFB;&#xFF1B;AST&#x7684;&#x6839;&#x5C06;&#x662F;&#x6574;&#x4E2A;&#x7A0B;&#x5E8F;&#xFF0C;&#x800C;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x5C06;&#x6709;&#x5B50;&#x8282;&#x70B9;&#x4EE3;&#x8868;&#x5B83;&#x7684;&#x7EC4;&#x6210;&#x90E8;&#x5206;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x5C0F;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code>if (a &lt; b) {
    c = 2;
    return c;
} else {
    c = 3;
}
</code></pre><p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;if&#x8BED;&#x53E5;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5C06;AST&#x7684;&#x6839;&#x6807;&#x8BB0;&#x4E3A; &quot;if statement&quot;&#x3002;&#x5B83;&#x6709;&#x4E09;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;condition (<code>a &lt; b</code>)</li>
<li>&#x8BED;&#x53E5;&#x5217;&#x8868;&#xFF1A;if body (<code>c = 2; return c;</code>)</li>
<li>&#x8BED;&#x53E5;&#x5217;&#x8868;&#xFF1A;else body (<code>c = 3;</code>)</li>
</ul>
<p>&#x8FD9;&#x4E9B;&#x8282;&#x70B9;&#x90FD;&#x53EF;&#x4EE5;&#x8FDB;&#x4E00;&#x6B65;&#x5206;&#x89E3;&#x3002;&#x4F8B;&#x5982;&#xFF0C;condition (<code>a &lt; b</code>)&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x4E00;&#x4E2A;&#x6709;&#x4E24;&#x4E2A;&#x64CD;&#x4F5C;&#x6570;&#xFF08;operand &#xFF09;&#x5B50;&#x8282;&#x70B9;&#x7684;<code>&quot;&lt;&quot;</code>&#x4E8C;&#x5143;&#x64CD;&#x4F5C;&#x7684;AST&#x8282;&#x70B9;&#xFF1A;</p>
<ul>
<li>first operand (variable <code>a</code>)</li>
<li>second operand (variable <code>b</code>)</li>
</ul>
<p>&#x4E00;&#x4E2A;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#xFF08;&#x5982;<code>c=2;</code>&#xFF09;&#x4E5F;&#x6709;&#x4E24;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#xFF1A;&#x88AB;&#x66F4;&#x65B0;&#x7684;&#x53D8;&#x91CF;&#xFF08;<code>c</code>&#xFF09;&#x548C;&#x8D4B;&#x503C;&#x7ED9;&#x5B83;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;<code>2</code>&#xFF09;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;<code>if body</code>&#x662F;&#x8BED;&#x53E5;&#x5217;&#x8868;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x6709;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x5B50;&#x8282;&#x70B9;--&#x6BCF;&#x4E2A;&#x8BED;&#x53E5;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#x3002;&#x5728;&#x672C;&#x4F8B;&#x4E2D;&#xFF0C;&#x5B83;&#x6709;&#x4E24;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x6709;&#x4E24;&#x6761;&#x8BED;&#x53E5;&#x3002;&#x8FD9;&#x4E9B;&#x5B50;&#x8282;&#x70B9;&#x662F;&#x6709;&#x5E8F;&#x7684;--<code>c=2;</code>&#x6392;&#x5728;<code>return c;</code>&#x4E4B;&#x524D;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x5C31;&#x662F;&#x8FD9;&#x6837;&#x6309;&#x5E8F;&#x6392;&#x5217;&#x7684;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x7684;&#x5B8C;&#x6574;AST&#xFF1A;</p>
<p><img src="pics/AST.svg" alt="Image of diagram; text outline follows"></p>
<ul>
<li>if statement<ul>
<li>condition: binary operation (&lt;)<ul>
<li>operand 1: variable a</li>
<li>operand 2: variable b</li>
</ul>
</li>
<li>if body: statement list<ul>
<li>statement 1: assignment<ul>
<li>variable: c</li>
<li>right-hand side: constant 2</li>
</ul>
</li>
<li>statement 2: return<ul>
<li>return value: variable c</li>
</ul>
</li>
</ul>
</li>
<li>else body: statement list<ul>
<li>statement 1: assignment<ul>
<li>variable: c</li>
<li>right-hand side: constant 3</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>&#x800C;&#x8FD9;&#x91CC;&#x662F;&#x6784;&#x9020;&#x8FD9;&#x4E2A;AST&#x7684;&#x4F2A;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>//create if condition
cond = BinaryOp(op=&apos;&gt;&apos;, operand_1=Var(a), operand_2=Var(b))

//create if body
assign = Assignment(var=Var(c), rhs=Const(2))
return = Return(val=Var(c))
if_body = [assign, return]

//create else body
assign_else = Assignment(var=Var(c), rhs=Const(3))
else_body = [assign_else]

//construct if statement
if = If(condition=cond, body=if_body, else=else_body)
</code></pre><p>&#x4E0D;&#x8FC7;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x62C5;&#x5FC3;&#x6761;&#x4EF6;&#xFF08;conditionals&#xFF09;&#x3001;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#xFF08;variable assignments&#xFF09;&#x6216;&#x4E8C;&#x8FDB;&#x5236;&#x64CD;&#x4F5C;&#x7B26;&#xFF08;binary operators&#xFF09;&#x3002;&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x652F;&#x6301;&#x7684;AST&#x8282;&#x70B9;&#x53EA;&#x6709;&#x7A0B;&#x5E8F;&#xFF08;programs&#xFF09;&#x3001;&#x51FD;&#x6570;&#x58F0;&#x660E;&#xFF08;function declarations&#xFF09;&#x3001;&#x8BED;&#x53E5;&#xFF08;statements&#xFF09;&#x548C;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;expressions&#xFF09;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x6211;&#x4EEC;&#x5BF9;<code>return_2.c</code>&#x7ED9;&#x51FA;&#x7684;AST&#x8282;&#x70B9;&#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code>program = Program(function_declaration)
function_declaration = Function(string, statement) //string is the function name
statement = Return(exp)
exp = Constant(int)
</code></pre><p>&#x73B0;&#x5728;&#xFF0C;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x7531;&#x4E00;&#x4E2A;&#x51FD;&#x6570;<code>main</code>&#x7EC4;&#x6210;&#x3002;&#x5728;&#x540E;&#x9762;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x628A;&#x4E00;&#x4E2A;&#x7A0B;&#x5E8F;&#x5B9A;&#x4E49;&#x4E3A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5217;&#x8868;&#x3002;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6709;&#x4E00;&#x4E2A;&#x540D;&#x79F0;&#xFF08;name&#xFF09;&#x548C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F53;&#xFF08;body&#xFF09;&#x3002;&#x4EE5;&#x540E;&#xFF0C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FD8;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5217;&#x8868;&#xFF08;list of arguments&#xFF09;&#x3002;&#x5728;&#x5B9E;&#x9645;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x5B58;&#x50A8;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#xFF08;return type&#xFF09;&#xFF0C;&#x4F46;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EA;&#x6709;&#x6574;&#x6570;&#x7C7B;&#x578B;&#x3002;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x53EA;&#x5305;&#x542B;&#x4E00;&#x6761;&#x5355;&#x4E00;&#x7684;&#x8BED;&#x53E5;&#xFF08;&#x540E;&#x7EED;&#x4F1A;&#x6269;&#x5C55;&#x4E3A;&#x8BED;&#x53E5;&#x5217;&#x8868;&#xFF09;&#x3002;&#x8BED;&#x53E5;&#x7684;&#x7C7B;&#x578B;&#x53EA;&#x6709;&#x4E00;&#x79CD;&#xFF1A;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#xFF08;return statement&#xFF09;&#x3002;&#x4EE5;&#x540E;&#x6211;&#x4EEC;&#x4F1A;&#x589E;&#x52A0;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x8BED;&#x53E5;&#xFF0C;&#x6BD4;&#x5982;&#x6761;&#x4EF6;&#xFF08;conditionals&#xFF09;&#x548C;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#xFF08;variable declarations&#xFF09;&#x3002;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x8BED;&#x53E5;&#x6709;&#x4E00;&#x4E2A;&#x5B50;&#x8BED;&#x53E5;&#xFF0C;&#x5373;&#x8868;&#x8FBE;&#x5F0F;--&#x8FD9;&#x5C31;&#x662F;&#x88AB;&#x8FD4;&#x56DE;&#x7684;&#x503C;&#x3002;&#x73B0;&#x5728;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x53EA;&#x80FD;&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x5E38;&#x91CF;&#x3002;&#x4EE5;&#x540E;&#x6211;&#x4EEC;&#x4F1A;&#x8BA9;&#x8868;&#x8FBE;&#x5F0F;&#x5305;&#x542B;&#x7B97;&#x672F;&#x8FD0;&#x7B97;&#xFF0C;&#x8FD9;&#x5C06;&#x4F7F;&#x6211;&#x4EEC;&#x80FD;&#x591F;&#x89E3;&#x6790;&#x50CF;<code>return 2+2;</code>&#x8FD9;&#x6837;&#x7684;&#x8BED;&#x53E5;&#x3002;</p>
<p>&#x5F53;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x65B0;&#x7684;&#x8BED;&#x8A00;&#x7ED3;&#x6784;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x66F4;&#x65B0;AST&#x8282;&#x70B9;&#x7684;&#x5B9A;&#x4E49;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x6700;&#x7EC8;&#x4F1A;&#x6DFB;&#x52A0;&#x4E00;&#x79CD;&#x65B0;&#x7684;&#x8BED;&#x53E5;&#x7C7B;&#x578B;&#xFF1A;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x3002;&#x5F53;&#x6211;&#x4EEC;&#x8FD9;&#x6837;&#x505A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x6211;&#x4EEC;&#x7684;<code>statement</code>&#x5B9A;&#x4E49;&#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5F62;&#x5F0F;&#x3002;</p>
<pre><code>statement = Return(exp) | Assign(variable, exp)
</code></pre><p>&#x8FD9;&#x91CC;&#x662F;return_2.c&#x7684;AST&#x56FE;&#x3002;</p>
<p><img src="pics/return_2_ast.svg" alt="Image of diagram; text outline follows"></p>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5F62;&#x5F0F;&#x5316;&#x7684;&#x8BED;&#x6CD5;&#xFF0C;&#x5B83;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x6807;&#x8BB0;&#x5982;&#x4F55;&#x7EC4;&#x5408;&#x6210;&#x8BED;&#x8A00;&#x6784;&#x9020;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x57FA;&#x4E8E;<a href="https://en.wikipedia.org/wiki/Backus-Naur_form-Naur_form" target="_blank">Backus-Naur Form</a>&#x6765;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code>&lt;program&gt; ::= &lt;function&gt;
&lt;function&gt; ::= &quot;int&quot; &lt;id&gt; &quot;(&quot; &quot;)&quot; &quot;{&quot; &lt;statement&gt; &quot;}&quot;
&lt;statement&gt; ::= &quot;return&quot; &lt;exp&gt; &quot;;&quot;
&lt;exp&gt; ::= &lt;int&gt;
</code></pre><p>&#x4E0A;&#x9762;&#x7684;&#x6BCF;&#x4E00;&#x884C;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x4EA7;&#x751F;&#x5F0F;&#xFF08;<em>production</em> &#xFF09;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x5982;&#x4F55;&#x4ECE;&#x4E00;&#x79CD;&#x5F62;&#x5F0F;&#x8BED;&#x8A00;&#xFF08;BNF&#xFF09;&#x7684;&#x6784;&#x9020;&#x548C;&#x6807;&#x8BB0;&#x6765;&#x5EFA;&#x7ACB;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x8BED;&#x8A00;&#xFF08;minidecaf&#xFF09;&#x7684;&#x6784;&#x9020;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x51FA;&#x73B0;&#x5728;&#x4EA7;&#x751F;&#x5F0F;&#x5DE6;&#x4FA7;&#x7684;&#x7B26;&#x53F7;&#xFF08;&#x5373;<code>&lt;program&gt;</code>&#x3001;<code>&lt;function&gt;</code>&#x3001;<code>&lt;statement&gt;</code>&#xFF09;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;&#xFF08;non-terminal symbol&#xFF09;&#x3002;&#x4E2A;&#x522B;&#x6807;&#x8BB0;&#xFF08;keywords&#x3001;id&#x3001;punctuation&#x7B49;&#xFF09;&#x662F;&#x7EC8;&#x7ED3;&#x7B26;&#xFF08;terminal symbols&#xFF09;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x867D;&#x7136;&#x8FD9;&#x4E2A;&#x8BED;&#x6CD5;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x4EC0;&#x4E48;&#x6837;&#x7684;&#x6807;&#x8BB0;&#x5E8F;&#x5217;&#x6784;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684;minidecaf&#x7A0B;&#x5E8F;&#xFF0C;&#x4F46;&#x5B83;*&#x6CA1;&#x6709;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x5230;&#x5E95;&#x5982;&#x4F55;&#x5C06;&#x8FD9;&#x4E2A;&#x7A0B;&#x5E8F;&#x8F6C;&#x5316;&#x4E3A;AST--&#x4F8B;&#x5982;&#xFF0C;&#x5728;AST&#x4E2D;&#x6CA1;&#x6709;&#x5BF9;&#x5E94;Constant&#x8282;&#x70B9;&#x7684;&#x4EA7;&#x751F;&#x5F0F;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#x6211;&#x4EEC;&#x7684;&#x8BED;&#x6CD5;&#xFF0C;&#x8BA9;&#x5E38;&#x91CF;&#x6709;&#x4E00;&#x4E2A;&#x4EA7;&#x751F;&#x5F0F;&#xFF0C;&#x4F46;&#x8FD9;&#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x7684;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x7684;&#x8BED;&#x6CD5;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x6BCF;&#x4E2A;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;&#x53EA;&#x6709;&#x4E00;&#x6761;&#x4EA7;&#x751F;&#x5F0F;&#x3002;&#x5728;&#x540E;&#x7EED;&#x8BD5;&#x9A8C;&#x4E2D;&#xFF0C;&#x4E00;&#x4E9B;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;&#x5C06;&#x6709;&#x591A;&#x4E2A;&#x4EA7;&#x751F;&#x5F0F;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x589E;&#x52A0;&#x4E86;&#x5BF9;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x7684;&#x652F;&#x6301;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x6709;&#x4EE5;&#x4E0B;&#x7684;&#x4EA7;&#x751F;&#x5F0F;&#x3002;</p>
<pre><code>&lt;statement&gt; ::= &quot;return&quot; &lt;int&gt; &quot;;&quot; | &quot;int&quot; &lt;id&gt; &quot;=&quot; &lt;int&gt; &quot;;&quot;
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part1-1-task.html" class="navigation navigation-prev " aria-label="Previous page: 任务">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part2-1-rdp.html" class="navigation navigation-next " aria-label="Next page: 递归下降分析">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"语法分析","level":"3.1.4","depth":2,"next":{"title":"递归下降分析","level":"3.1.5","depth":2,"path":"docs/lab1/part2-1-rdp.md","ref":"docs/lab1/part2-1-rdp.md","articles":[]},"previous":{"title":"任务","level":"3.1.3","depth":2,"path":"docs/lab1/part1-1-task.md","ref":"docs/lab1/part1-1-task.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","mermaid-gb3","codeblock-label","code","search-pro","click-reveal","expandable-chapters-interactive","localized-footer"],"pluginsConfig":{"chapter-fold":{},"emphasize":{},"codeblock-label":{},"search-pro":{},"search":{},"localized-footer":{"filename":"gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":1},"click-reveal":{},"highlight":{},"mermaid-gb3":{},"expandable-chapters-interactive":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"docs/lab1/part2-parse.md","mtime":"2020-08-21T06:06:09.408Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-26T15:46:29.226Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-click-reveal/click_reveal.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

